<?php
/**
 * 人员管理
 * Created by PhpStorm.
 * User: 北山道人
 * Date: 2018/6/29
 * Time: 11:51
 */
namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\AuthGroup;
use think\Db;
use think\Request;
use think\Validate;

class Person extends Common
{

    public function query(Request $request){
        $input = $request->param();
        if(empty($input['gid'])) return ['code'=>0,'msg'=>'gid参数错误'];
        $rows = db('admin')->alias('a')->where(function ($query)use($input){
            if(!empty($input['gid']))
                $query->where('a.group_id',$input['gid']);
            if(isset($input['is_group']))
                $query->where('a.is_group',$input['is_group']);
            $query->where('a.is_part',0);
        })
            ->join('auth_group ag','a.group_id=ag.group_id','left')
        ->field(['a.*,ag.title'])
        ->select();
        return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$rows,'rel'=>1];
    }

    /**
     * 人员管理页面
     * @return mixed
     */
    public function indexUI(Request $request){

        if($request->isAjax()){
            $val=input('val');
            $url['val'] = $val;
            $this->assign('testval',$val);
            $map='';
            if($val){
                $map['username|email|tel']= array('like',"%".$val."%");
            }
            if (session('aid')!=1){
                $map['admin_id']=session('aid');
            }
            $list=Db::table(config('database.prefix').'admin')->alias('a')
                ->join(config('database.prefix').'auth_group ag','a.group_id = ag.group_id','left')
                ->field('a.*,ag.title')
                ->where($map)
                ->select();
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list,'rel'=>1];
        }
        return $this->fetch('index');
    }

    /**
     * 添加页面
     * @return mixed
     */
    public function addUI(){

        if(request()->isPost()){
            $data = input('post.');
            $check_user = Admin::get(['username'=>$data['username']]);
            if ($check_user) {
                return $result = ['code'=>0,'msg'=>'用户已存在，请重新输入用户名!'];
            }
            $data['pwd'] = input('post.pwd', '', 'md5');
            $data['add_time'] = time();
            $data['ip'] = request()->ip();
            //验证
            $msg = $this->validate($data,'app\admin\validate\Admin');
            if($msg!='true'){
                return json(['code'=>0,'msg'=>$msg]);
            }
            //单独验证密码
            $checkPwd = Validate::make([input('post.pwd')=>'require']);
            if (false === $checkPwd) {
                return json(['code'=>0,'msg'=>'密码不能为空！']);
            }
            //添加
            if (Admin::create($data)) {
                return json(['code'=>1,'msg'=>'人员添加成功!','url'=>url('indexUI')]);
            } else {
                return json(['code'=>0,'msg'=>'人员添加失败!']);
            }
        }
        $auth_group=db('auth_group')->select();
        $this->assign('authGroup',$auth_group);
        $this->assign('info','null');
        $this->assign('selected', 'null');
        return $this->fetch('add');
    }

    /**
     * 编辑页面
     */
    public function editUI(){
        if(request()->isPost()){
            $data = input('post.');
            $pwd=input('post.pwd');
            $map[] = ['admin_id','<>',$data['admin_id']];
            $where['admin_id'] = $data['admin_id'];

            if($data['username']){
                $map[] = ['username','=',$data['username']];
                $check_user = Admin::where($map)->find();
                if ($check_user) {
                    return $result = ['code'=>0,'msg'=>'用户已存在，请重新输入用户名!'];
                }
            }
            if ($pwd){
                $data['pwd']=input('post.pwd','','md5');
            }else{
                unset($data['pwd']);
            }
            $msg = $this->validate($data,'app\admin\validate\Admin');
            if($msg!='true'){
                return $result = ['code'=>0,'msg'=>$msg];
            }
            Admin::update($data,$where);
            session('username',$data['username']);
            $avatar = $data['avatar']==''?'/public/static/admin/images/0.jpg':$data['avatar'];
            session('avatar',$avatar);
            return $result = ['code'=>1,'msg'=>'管理员修改成功!','url'=>url('indexUI')];
        }else{
            $auth_group = AuthGroup::all();
            $admin = new Admin();
            $info = $admin->getInfo(input('admin_id'));
            $this->assign('info', json_encode($info,JSON_UNESCAPED_UNICODE));
            $this->assign('authGroup',$auth_group);
            $this->assign('title',lang('edit').lang('admin'));
            return $this->fetch('add');
        }
    }


}